package com.sokobanboxpro.game.managers;

import com.sokobanboxpro.game.Application;
import com.sokobanboxpro.game.ApplicationState;
import com.sokobanboxpro.game.entity.*;
import com.sokobanboxpro.game.exception.ConfigValueNotFoundException;
import com.sokobanboxpro.game.entity.ImageObj;
import com.sokobanboxpro.game.entity.Text;
import com.sokobanboxpro.game.system.Config;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

/**
 * Графический менеджер
 * @author Esr
 */
public class GraphicsManager {

		/**
		 * Высота одного блока
		 */
		private static int heightBlock;
		/**
		 * Ширена одного блока
		 */
		private static int widthBlock;
		/**
		 * Изображение стены
		 */
		private static Image wallImage;
		/**
		 * Изображение игрока
		 */
		private static Image playerImage;
		/**
		 * Изображение точки
		 */
		private static Image pointImage;
		/**
		 * Изображение ящика
		 */
		private static Image boxImage;
		/**
		 * Изображение ящика помешеного на точку
		 */
		private static Image boxPointImage;
		/**
		 * Изображение игрока стоящего на точне
		 */
		private static Image playerPointImage;
		/**
		 * Изображение пола
		 */
		private static Image floorImage;

		/**
		 * Создание
		 * @throws ConfigValueNotFoundException Не был найден путь до файла изображений
		 */
		public static void init() throws ConfigValueNotFoundException {
				
				try {

						Image fullImage	 = new Image(Config.toStr("LevelImage"));
				
						heightBlock = fullImage.getHeight() / 4;
						widthBlock = fullImage.getWidth() / 4;

						playerImage = fullImage.getSubImage(0, heightBlock, widthBlock, heightBlock);
						pointImage = fullImage.getSubImage(widthBlock * 2, heightBlock, widthBlock, heightBlock);
						boxImage = fullImage.getSubImage(widthBlock, heightBlock, widthBlock, heightBlock);
						boxPointImage = fullImage.getSubImage(widthBlock * 2, heightBlock * 2, widthBlock, heightBlock);
						playerPointImage = fullImage.getSubImage(widthBlock * 2, heightBlock * 3, widthBlock, heightBlock);
						wallImage = fullImage.getSubImage(widthBlock, 0, widthBlock, heightBlock);
						floorImage = fullImage.getSubImage(widthBlock * 2, 0, widthBlock, heightBlock);
				} catch (SlickException ex) {
				}

		}

		/**
		 * Прорисовать меню
		 * @param gc
		 * @param grphcs 
		 */
		public static void renderMenu(GameContainer gc, Graphics grphcs) {

				ImageObj[] images = ActionMenuManager.getCurrentMenu().getImageObj();
				Text[] texts = ActionMenuManager.getCurrentMenu().getText();

				for (ImageObj image : images) {
						image.getImage().draw(image.getX(), image.getY());
				}

				for (Text text : texts) {
						grphcs.drawString(text.toString(), text.getX(), text.getY());
				}

		}

		/**
		 * Прорисовать уровень
		 * @param gc
		 * @param grphcs 
		 */
		public static void renderLevel(GameContainer gc, Graphics grphcs) {

				Camera camera = ActionGameManager.game.getGameplay().getCamera();

				Box[] boxes = ActionGameManager.game.getCurrentLevel().getBoxes();
				Player player = ActionGameManager.game.getCurrentLevel().getPlayer();
				Point[] points = ActionGameManager.game.getCurrentLevel().getPoints();
				Wall[] walls = ActionGameManager.game.getCurrentLevel().getWalls();
				Floor[] floors = ActionGameManager.game.getCurrentLevel().getFloors();

				ImageObj[] interfaceImages = ActionGameManager.game.getInterface().getImageObjs();
				Text[] interfaceTexts = ActionGameManager.game.getInterface().getTexts();

				for (Box box : boxes) {

						boxImage.draw((box.getX() * widthBlock + camera.getX()) * camera.getZoom(), (box.getY() * heightBlock + camera.getY()) * camera.getZoom(), camera.getZoom());
				}
				for (Point point : points) {
						pointImage.draw((point.getX() * widthBlock + camera.getX()) * camera.getZoom(), (point.getY() * heightBlock + camera.getY()) * camera.getZoom(), camera.getZoom());
				}
				for (Wall wall : walls) {
						wallImage.draw((wall.getX() * widthBlock + camera.getX()) * camera.getZoom(), (wall.getY() * heightBlock + camera.getY()) * camera.getZoom(), camera.getZoom());
				}
				for (Floor floor : floors) {
						floorImage.draw((floor.getX() * widthBlock + camera.getX()) * camera.getZoom(), (floor.getY() * heightBlock + camera.getY()) * camera.getZoom(), camera.getZoom());
				}
				playerImage.draw((player.getX() * widthBlock + camera.getX()) * camera.getZoom(), (player.getY() * heightBlock + camera.getY()) * camera.getZoom(), camera.getZoom());

				for (ImageObj image : interfaceImages) {
						image.getImage().draw(image.getX(), image.getY());
				}

				for (Text text : interfaceTexts) {
						grphcs.drawString(text.toString(), text.getX(), text.getY());
				}

		}

		/**
		 * Перерисовать картику
		 * @param gc
		 * @param grphcs 
		 */
		public static void render(GameContainer gc, Graphics grphcs) {

				if (Application.applicationState == ApplicationState.MENU) {
						renderMenu(gc, grphcs);
				} else {
						renderLevel(gc, grphcs);
				}

		}

}
